package cn.pegasus.teamwork.util;

import java.io.ByteArrayOutputStream;
import java.io.UnsupportedEncodingException;
import java.security.SecureRandom;
import java.text.ParseException;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;

public class HexUtil {
	static String digital = "0123456789ABCDEF"; 
	public static String byteToHex(byte[] data){ 
		ByteArrayOutputStream bos = new ByteArrayOutputStream();
		for (int i = 0; i < data.length; i++) {
            int    v = data[i] & 0xff;

            bos.write(digital.charAt(v >>> 4));
            bos.write(digital.charAt(v & 0xf));
        }
        return bos.toString();
	}
	
	public static byte[] hexToByte(String hex) throws UnsupportedEncodingException{
		final byte[] encodingTable =
            {
                (byte)'0', (byte)'1', (byte)'2', (byte)'3', (byte)'4', (byte)'5', (byte)'6', (byte)'7',
                (byte)'8', (byte)'9', (byte)'a', (byte)'b', (byte)'c', (byte)'d', (byte)'e', (byte)'f'
            };
        
        byte[] decodingTable = new byte[128];

     
        for (int i = 0; i < encodingTable.length; i++) {
                decodingTable[encodingTable[i]] = (byte)i;
        }
            
        decodingTable['A'] = decodingTable['a'];
        decodingTable['B'] = decodingTable['b'];
        decodingTable['C'] = decodingTable['c'];
        decodingTable['D'] = decodingTable['d'];
        decodingTable['E'] = decodingTable['e'];
        decodingTable['F'] = decodingTable['f'];
    
        int len = hex.length();
        byte[] hex_byte = hex.getBytes("UTF-8");
      
        int retLen = len / 2;
        byte[] ret = new byte[retLen];
        int j = 0;
        for(int i = 0;i < len;i += 2){
            ret[j] =  decodingTable[hex_byte[i]];
            ret[j] =  (byte) ((ret[j] << 4) | decodingTable[hex_byte[i + 1]]);
            j++;
        }
        return ret;
	}
	
	 	private static final String passwd="30820276020100300D06092A864886F70D0101010500048202603082025C02010002818100A58DD2240BC861A1AF3A7CA876E3080582A0E97BC07F5FE2C53E595C95A1F35A634784AD48B910EF3DC5823BEF553A816C1035FCD3D3355EDC063348AD4D1AD074015D7A15E8A757D82195CD568C20E4D034DB206B59B9BB8B43DD43E68E70648F92C5F3B28EC3CF9D11DBB50292B94661932F6055489F8DAF852A0691854E5F02030100010281807003F876893E68C8D45E57704918AE19C45C09FEC0ADAE7C2A8758416BC12B7E32047702245D8BD6C44DFA238179825F8575A681CDF56F7C22955367CE52885C01BD2B0E2CA5156DD53A6BEEB1F1061B13CA45EEB43405A41911759CC8CD8B4F68548A34C22EF3046FAE04524F96443C0A1AE71B2127E9BB5D8DFF8DD0815879024100DA75F72C0923669FC7872D89F99E9218720A44EC1D143EAE1D529409ACA34A1FDBD8F95D50015B7C0951749F11E4FE5C2DBCD6446B898C1C9D4822F22DCB7893024100C2007F881CCA22F101F7784FD868032C66439BFA45E272D03DC1A15BFF37098919558122D523DC73A329D0AB611E95D2073A7134C2627699A4BB0CBA15CDEE85024100C9D05691E0B34A77EABCAF4E1A42EA16192BF175EA7B2C586F103D601E930EC415B600CFBA3C36C6845932924CD6AE407BA44A91148311E3D14CB5B3E2A8686F024039CAB23316F8744E3D5ABE2173031126F1D3E42C3B134946958F10057AD2A7698AFD78B9AAAAC32A4389DA5AA354DC26B3F689F0C0935A674CB658053B279C8D0240019C28B53A20C794C37DB7FF07C5B7FB894DB53338CE52733E70A29DA70C5E63EF90C0A9A08D548F10760C6EBF7A1AB7BD526B8E42DB759EFBA2E07DF5835E54";
	    private static final String sss="79A4E91CB9F5DB88EA7A7EDC8FA4D2FA6EAE5C53774A16B995226A27D90DB0FD68B09A604290572DAF6E6F1F284F8678B9691430E3954D13A2193C9219095986A0D7BD189FC8AEE659B402AD7AD2C39C086B730C882E57A53470805F0BABBA98DC31347EA150E1F8AC1759FD1C99A0C1CDDF7B92930846DE1C9A952381E48787BB2C7751BBB7B05E2CD0FA38B7A93FE51DB10F283A8495F1524CF0CB2C51A63DD6B9AF70DA8EE32D524AE3DB9E3D79E59969D8EC6A92C1C937498737C7DEE24BF123779F4A8AA13F5F5A1B06B46AFD1A879FC2D55DBCEE84E920A6902523A4C60F60F82F1B36E7E1AE04998B4806233BE0709E426983D9DEAD9112D4AE5425B16B38F2AA5BAD60B8FAE3CCF3F2D1C42CA92EC4C8296849DC9023FD2B63DB33DDE67F319D019067A4F5D73B6C2CF0E1CBAEFBE320B65C46015EC3615D23F857A094101D511BBC6175445B5FBADA5C76D3CA981D684C2F23F7E7FB4740DC3B2EDD886809CCAC395FF4862B3CBEB121B70D40A6CD2415EC9EAD6E2894757EBC414B";
	    public static void main(String[] args) {
	    	String json="{'count':4,'errmsg':'OK','items':[{'mobile':'18760031673','lcs_uid':'','status':'12','end_time':'2016-06-01 10:25:58'},{'mobile':'13620060097','lcs_uid':'','status':'12','end_time':'2016-06-01 14:13:58'},{'mobile':'18064251393','lcs_uid':'','status':'12','end_time':'2016-06-01 09:33:57'},{'mobile':'18825301598','lcs_uid':'','status':'12','end_time':'2016-06-02 09:38:17'}]}";
	    	try {
//			String info=decrypt(sss, passwd);
			ParseJson(json);
//			System.out.println(info);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	    
	    public static String decrypt(String encryptStr,String passwd) throws Exception{
			KeyGenerator kgen = KeyGenerator.getInstance("AES");  
	        kgen.init(128, new SecureRandom(passwd.getBytes("UTF-8")));  
	        SecretKey secretKey = kgen.generateKey();  
	        byte[] enCodeFormat = secretKey.getEncoded();  
	        SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");              
	        Cipher cipher = Cipher.getInstance("AES");// 创建密码器   
	        cipher.init(Cipher.DECRYPT_MODE, key);// 初始化   
	        byte[] srcBytes = cipher.doFinal(HexUtil.hexToByte(encryptStr));
	        return new String(srcBytes,"UTF-8");
		}
	    public static void ParseJson(String jsonString) throws JSONException, ParseException {
			JSONObject jo = JSONObject.parseObject(jsonString);
			JSONArray jsonArray = jo.getJSONArray("items");
			for (int i = 0; i < jsonArray.size(); i++) {
				JSONObject jo2 = jsonArray.getJSONObject(i);
				System.out.println(jo2.getString("mobile"));
				System.out.println(jo2.getString("end_time"));

			}
		}
}
